home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / keymap.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  68KB  |  2,322 lines

  1. /* Manipulation of keymaps
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #undef NULL
  24. #include "lisp.h"
  25. #include "commands.h"
  26. #include "buffer.h"
  27. #include "keyboard.h"
  28. #include "blockinput.h"
  29. #include "frame.h"
  30. #include "termhooks.h"
  31.  
  32. #ifdef STDC_HEADERS
  33. #include <stdlib.h>
  34. #endif
  35. #include "keymap_p.h"
  36. #include "xdisp_p.h"
  37. #include "insdel_p.h"
  38. #include "alloca_p.h"
  39. static Lisp_Object define_as_prefix _P_((Lisp_Object keymap, Lisp_Object c));
  40. static int ascii_sequence_p _P_((Lisp_Object seq));
  41. static Lisp_Object where_is_string _P_((Lisp_Object definition));
  42. static Lisp_Object describe_buffer_bindings _P_((Lisp_Object arg));
  43. static void describe_command _P_((Lisp_Object definition));
  44. static void describe_map _P_((Lisp_Object map, Lisp_Object keys, int partial,
  45.                               Lisp_Object shadow));
  46. static Lisp_Object shadow_lookup _P_((Lisp_Object shadow, Lisp_Object key,
  47.                                       Lisp_Object flag));
  48. static void describe_map_2 _P_((register Lisp_Object keymap,
  49.                                 Lisp_Object elt_prefix,
  50.                                 void (*elt_describer)(Lisp_Object description),
  51.                                 int partial, Lisp_Object shadow));
  52. static void describe_vector_princ _P_((Lisp_Object elt));
  53. static void apropos_accum _P_((Lisp_Object symbol, Lisp_Object string));
  54.  
  55. #ifndef min
  56. #define min(a, b) ((a) < (b) ? (a) : (b))
  57. #endif
  58.  
  59. /* The number of elements in keymap vectors.  */
  60. #define DENSE_TABLE_SIZE (0200)
  61.  
  62. /* Actually allocate storage for these variables */
  63.  
  64. Lisp_Object current_global_map;    /* Current global keymap */
  65.  
  66. Lisp_Object global_map;        /* default global key bindings */
  67.  
  68. Lisp_Object meta_map;        /* The keymap used for globally bound
  69.                    ESC-prefixed default commands */
  70.  
  71. Lisp_Object control_x_map;    /* The keymap used for globally bound
  72.                    C-x-prefixed default commands */
  73.  
  74. /* was MinibufLocalMap */
  75. Lisp_Object Vminibuffer_local_map;
  76.                 /* The keymap used by the minibuf for local
  77.                    bindings when spaces are allowed in the
  78.                    minibuf */
  79.  
  80. /* was MinibufLocalNSMap */
  81. Lisp_Object Vminibuffer_local_ns_map;            
  82.                 /* The keymap used by the minibuf for local
  83.                    bindings when spaces are not encouraged
  84.                    in the minibuf */
  85.  
  86. /* keymap used for minibuffers when doing completion */
  87. /* was MinibufLocalCompletionMap */
  88. Lisp_Object Vminibuffer_local_completion_map;
  89.  
  90. /* keymap used for minibuffers when doing completion and require a match */
  91. /* was MinibufLocalMustMatchMap */
  92. Lisp_Object Vminibuffer_local_must_match_map;
  93.  
  94. /* Alist of minor mode variables and keymaps.  */
  95. Lisp_Object Vminor_mode_map_alist;
  96.  
  97. /* Keymap mapping ASCII function key sequences onto their preferred forms.
  98.    Initialized by the terminal-specific lisp files.  See DEFVAR for more
  99.    documentation.  */
  100. Lisp_Object Vfunction_key_map;
  101.  
  102. Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii;
  103.  
  104. /* A char with the CHAR_META bit set in a vector or the 0200 bit set
  105.    in a string key sequence is equivalent to prefixing with this
  106.    character.  */
  107. extern Lisp_Object meta_prefix_char;
  108.  
  109.  
  110. /* Keymap object support - constructors and predicates.            */
  111.  
  112. DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
  113.   "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\
  114. VECTOR is a vector which holds the bindings for the ASCII\n\
  115. characters.  ALIST is an assoc-list which holds bindings for function keys,\n\
  116. mouse events, and any other things that appear in the input stream.\n\
  117. All entries in it are initially nil, meaning \"command undefined\".\n\n\
  118. The optional arg STRING supplies a menu name for the keymap\n\
  119. in case you use it as a menu with `x-popup-menu'.")
  120.   (string)
  121.      Lisp_Object string;
  122. {
  123.   Lisp_Object tail;
  124.   if (!NILP (string))
  125.     tail = Fcons (string, Qnil);
  126.   else
  127.     tail = Qnil;
  128.   return Fcons (Qkeymap,
  129.         Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE), Qnil),
  130.                tail));
  131. }
  132.  
  133. DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
  134.   "Construct and return a new sparse-keymap list.\n\
  135. Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
  136. which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
  137. which binds the function key or mouse event SYMBOL to DEFINITION.\n\
  138. Initially the alist is nil.\n\n\
  139. The optional arg STRING supplies a menu name for the keymap\n\
  140. in case you use it as a menu with `x-popup-menu'.")
  141.   (string)
  142.      Lisp_Object string;
  143. {
  144.   if (!NILP (string))
  145.     return Fcons (Qkeymap, Fcons (string, Qnil));
  146.   return Fcons (Qkeymap, Qnil);
  147. }
  148.  
  149. /* This function is used for installing the standard key bindings
  150.    at initialization time.
  151.  
  152.    For example:
  153.  
  154.    initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark");  */
  155.  
  156. void
  157. initial_define_key (keymap, key, defname)
  158.      Lisp_Object keymap;
  159.      int key;
  160.      char *defname;
  161. {
  162.   store_in_keymap (keymap, make_number (key), intern (defname));
  163. }
  164.  
  165. void
  166. initial_define_lispy_key (keymap, keyname, defname)
  167.      Lisp_Object keymap;
  168.      char *keyname;
  169.      char *defname;
  170. {
  171.   store_in_keymap (keymap, intern (keyname), intern (defname));
  172. }
  173.  
  174. /* Define character fromchar in map frommap as an alias for character
  175.    tochar in map tomap.  Subsequent redefinitions of the latter WILL
  176.    affect the former. */
  177.  
  178. #if 0
  179. void
  180. synkey (frommap, fromchar, tomap, tochar)
  181.      struct Lisp_Vector *frommap, *tomap;
  182.      int fromchar, tochar;
  183. {
  184.   Lisp_Object v, c;
  185.   XSET (v, Lisp_Vector, tomap);
  186.   XFASTINT (c) = tochar;
  187.   frommap->contents[fromchar] = Fcons (v, c);
  188. }
  189. #endif /* 0 */
  190.  
  191. DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
  192.   "Return t if ARG is a keymap.\n\
  193. \n\
  194. A keymap is a list (keymap . ALIST),\n\
  195. or a symbol whose function definition is a keymap is itself a keymap.\n\
  196. ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
  197. a vector of densely packed bindings for small character codes\n\
  198. is also allowed as an element.")
  199.   (object)
  200.      Lisp_Object object;
  201. {
  202.   return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
  203. }
  204.  
  205. /* Check that OBJECT is a keymap (after dereferencing through any
  206.    symbols).  If it is, return it.
  207.  
  208.    If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
  209.    is an autoload form, do the autoload and try again.
  210.  
  211.    ERROR controls how we respond if OBJECT isn't a keymap.
  212.    If ERROR is non-zero, signal an error; otherwise, just return Qnil.
  213.  
  214.    Note that most of the time, we don't want to pursue autoloads.
  215.    Functions like Faccessible_keymaps which scan entire keymap trees
  216.    shouldn't load every autoloaded keymap.  I'm not sure about this,
  217.    but it seems to me that only read_key_sequence, Flookup_key, and
  218.    Fdefine_key should cause keymaps to be autoloaded.  */
  219.  
  220. Lisp_Object
  221. get_keymap_1 (object, error, autoload)
  222.      Lisp_Object object;
  223.      int error, autoload;
  224. {
  225.   Lisp_Object tem;
  226.  
  227.  autoload_retry:
  228.   tem = indirect_function (object);
  229.   if (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap))
  230.     return tem;
  231.  
  232.   /* Should we do an autoload?  Autoload forms for keymaps have
  233.      Qkeymap as their fifth element.  */
  234.   if (autoload
  235.       && XTYPE (object) == Lisp_Symbol
  236.       && CONSP (tem)
  237.       && EQ (XCONS (tem)->car, Qautoload))
  238.     {
  239.       Lisp_Object tail;
  240.  
  241.       tail = Fnth (make_number (4), tem);
  242.       if (EQ (tail, Qkeymap))
  243.     {
  244.       struct gcpro gcpro1, gcpro2;
  245.  
  246.       GCPRO2 (tem, object);
  247.       do_autoload (tem, object);
  248.       UNGCPRO;
  249.  
  250.       goto autoload_retry;
  251.     }
  252.     }
  253.  
  254.   if (error)
  255.     wrong_type_argument (Qkeymapp, object);
  256.   else
  257.     return Qnil;
  258. }
  259.  
  260.  
  261. /* Follow any symbol chaining, and return the keymap denoted by OBJECT.
  262.    If OBJECT doesn't denote a keymap at all, signal an error.  */
  263. Lisp_Object
  264. get_keymap (object)
  265.      Lisp_Object object;
  266. {
  267.   return get_keymap_1 (object, 0, 0);
  268. }
  269.  
  270.  
  271. /* Look up IDX in MAP.  IDX may be any sort of event.
  272.    Note that this does only one level of lookup; IDX must be a single
  273.    event, not a sequence. 
  274.  
  275.    If T_OK is non-zero, bindings for Qt are treated as default
  276.    bindings; any key left unmentioned by other tables and bindings is
  277.    given the binding of Qt.  
  278.  
  279.    If T_OK is zero, bindings for Qt are not treated specially.
  280.  
  281.    If NOINHERIT, don't accept a subkeymap found in an inherited keymap.  */
  282.  
  283. Lisp_Object
  284. access_keymap (map, idx, t_ok, noinherit)
  285.      Lisp_Object map;
  286.      Lisp_Object idx;
  287.      int t_ok;
  288.      int noinherit;
  289. {
  290.   int noprefix = 0;
  291.   Lisp_Object val;
  292.  
  293.   /* If idx is a list (some sort of mouse click, perhaps?),
  294.      the index we want to use is the car of the list, which
  295.      ought to be a symbol.  */
  296.   idx = EVENT_HEAD (idx);
  297.  
  298.   /* If idx is a symbol, it might have modifiers, which need to
  299.      be put in the canonical order.  */
  300.   if (XTYPE (idx) == Lisp_Symbol)
  301.     idx = reorder_modifiers (idx);
  302.   else if (INTEGERP (idx))
  303.     /* Clobber the high bits that can be present on a machine
  304.        with more than 24 bits of integer.  */
  305.     XFASTINT (idx) = XINT (idx) & (CHAR_META | (CHAR_META - 1));
  306.  
  307.   {
  308.     Lisp_Object tail;
  309.     Lisp_Object t_binding = Qnil;
  310.  
  311.     for (tail = map; CONSP (tail); tail = XCONS (tail)->cdr)
  312.       {
  313.     Lisp_Object binding = XCONS (tail)->car;
  314.  
  315.     switch (XTYPE (binding))
  316.       {
  317.       case Lisp_Symbol:
  318.         /* If NOINHERIT, stop finding prefix definitions
  319.            after we pass a second occurrence of the `keymap' symbol.  */
  320.         if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
  321.           noprefix = 1;
  322.         break;
  323.  
  324.       case Lisp_Cons:
  325.         if (EQ (XCONS (binding)->car, idx))
  326.           {
  327.         val = XCONS (binding)->cdr;
  328.         if (noprefix && CONSP (val) && EQ (XCONS (val)->car, Qkeymap))
  329.           return Qnil;
  330.         return val;
  331.           }
  332.         if (t_ok && EQ (XCONS (binding)->car, Qt))
  333.           t_binding = XCONS (binding)->cdr;
  334.         break;
  335.  
  336.       case Lisp_Vector:
  337.         if (XTYPE (idx) == Lisp_Int
  338.         && XINT (idx) >= 0
  339.         && XINT (idx) < XVECTOR (binding)->size)
  340.           {
  341.         val = XVECTOR (binding)->contents[XINT (idx)];
  342.         if (noprefix && CONSP (val) && EQ (XCONS (val)->car, Qkeymap))
  343.           return Qnil;
  344.         return val;
  345.           }
  346.         break;
  347.       }
  348.  
  349.     QUIT;
  350.       }
  351.  
  352.     return t_binding;
  353.   }
  354. }
  355.  
  356. /* Given OBJECT which was found in a slot in a keymap,
  357.    trace indirect definitions to get the actual definition of that slot.
  358.    An indirect definition is a list of the form
  359.    (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
  360.    and INDEX is the object to look up in KEYMAP to yield the definition.
  361.  
  362.    Also if OBJECT has a menu string as the first element,
  363.    remove that.  Also remove a menu help string as second element.  */
  364.  
  365. Lisp_Object
  366. get_keyelt (object)
  367.      register Lisp_Object object;
  368. {
  369.   while (1)
  370.     {
  371.       register Lisp_Object map, tem;
  372.  
  373.       /* If the contents are (KEYMAP . ELEMENT), go indirect.  */
  374.       map = get_keymap_1 (Fcar_safe (object), 0, 0);
  375.       tem = Fkeymapp (map);
  376.       if (!NILP (tem))
  377.     object = access_keymap (map, Fcdr (object), 0, 0);
  378.       
  379.       /* If the keymap contents looks like (STRING . DEFN),
  380.      use DEFN.
  381.      Keymap alist elements like (CHAR MENUSTRING . DEFN)
  382.      will be used by HierarKey menus.  */
  383.       else if (XTYPE (object) == Lisp_Cons
  384.            && XTYPE (XCONS (object)->car) == Lisp_String)
  385.     {
  386.       object = XCONS (object)->cdr;
  387.       /* Also remove a menu help string, if any,
  388.          following the menu item name.  */
  389.       if (XTYPE (object) == Lisp_Cons
  390.           && XTYPE (XCONS (object)->car) == Lisp_String)
  391.         object = XCONS (object)->cdr;
  392.     }
  393.  
  394.       else
  395.     /* Anything else is really the value.  */
  396.     return object;
  397.     }
  398. }
  399.  
  400. Lisp_Object
  401. store_in_keymap (keymap, idx, def)
  402.      Lisp_Object keymap;
  403.      register Lisp_Object idx;
  404.      register Lisp_Object def;
  405. {
  406.   if (XTYPE (keymap) != Lisp_Cons
  407.       || ! EQ (XCONS (keymap)->car, Qkeymap))
  408.     error ("attempt to define a key in a non-keymap");
  409.  
  410.   /* If idx is a list (some sort of mouse click, perhaps?),
  411.      the index we want to use is the car of the list, which
  412.      ought to be a symbol.  */
  413.   idx = EVENT_HEAD (idx);
  414.  
  415.   /* If idx is a symbol, it might have modifiers, which need to
  416.      be put in the canonical order.  */
  417.   if (XTYPE (idx) == Lisp_Symbol)
  418.     idx = reorder_modifiers (idx);
  419.   else if (INTEGERP (idx))
  420.     /* Clobber the high bits that can be present on a machine
  421.        with more than 24 bits of integer.  */
  422.     XFASTINT (idx) = XINT (idx) & (CHAR_META | (CHAR_META - 1));
  423.  
  424.   /* Scan the keymap for a binding of idx.  */
  425.   {
  426.     Lisp_Object tail;
  427.  
  428.     /* The cons after which we should insert new bindings.  If the
  429.        keymap has a table element, we record its position here, so new
  430.        bindings will go after it; this way, the table will stay
  431.        towards the front of the alist and character lookups in dense
  432.        keymaps will remain fast.  Otherwise, this just points at the
  433.        front of the keymap.  */
  434.     Lisp_Object insertion_point = keymap;
  435.  
  436.     for (tail = XCONS (keymap)->cdr; CONSP (tail); tail = XCONS (tail)->cdr)
  437.       {
  438.     Lisp_Object elt = XCONS (tail)->car;
  439.  
  440.     switch (XTYPE (elt))
  441.       {
  442.       case Lisp_Vector:
  443.         if (XTYPE (idx) == Lisp_Int
  444.         && XINT (idx) >= 0 && XINT (idx) < XVECTOR (elt)->size)
  445.           {
  446.         XVECTOR (elt)->contents[XFASTINT (idx)] = def;
  447.         return def;
  448.           }
  449.         insertion_point = tail;
  450.         break;
  451.  
  452.       case Lisp_Cons:
  453.         if (EQ (idx, XCONS (elt)->car))
  454.           {
  455.         XCONS (elt)->cdr = def;
  456.         return def;
  457.           }
  458.         break;
  459.  
  460.       case Lisp_Symbol:
  461.         /* If we find a 'keymap' symbol in the spine of KEYMAP,
  462.                then we must have found the start of a second keymap
  463.                being used as the tail of KEYMAP, and a binding for IDX
  464.                should be inserted before it.  */
  465.         if (EQ (elt, Qkeymap))
  466.           goto keymap_end;
  467.         break;
  468.       }
  469.  
  470.     QUIT;
  471.       }
  472.  
  473.   keymap_end:
  474.     /* We have scanned the entire keymap, and not found a binding for
  475.        IDX.  Let's add one.  */
  476.     XCONS (insertion_point)->cdr =
  477.       Fcons (Fcons (idx, def), XCONS (insertion_point)->cdr);
  478.   }
  479.       
  480.   return def;
  481. }
  482.  
  483.  
  484. DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
  485.   "Return a copy of the keymap KEYMAP.\n\
  486. The copy starts out with the same definitions of KEYMAP,\n\
  487. but changing either the copy or KEYMAP does not affect the other.\n\
  488. Any key definitions that are subkeymaps are recursively copied.\n\
  489. However, a key definition which is a symbol whose definition is a keymap\n\
  490. is not copied.")
  491.   (keymap)
  492.      Lisp_Object keymap;
  493. {
  494.   register Lisp_Object copy, tail;
  495.  
  496.   copy = Fcopy_alist (get_keymap (keymap));
  497.  
  498.   for (tail = copy; CONSP (tail); tail = XCONS (tail)->cdr)
  499.     {
  500.       Lisp_Object elt = XCONS (tail)->car;
  501.  
  502.       if (XTYPE (elt) == Lisp_Vector)
  503.     {
  504.       int i;
  505.  
  506.       elt = Fcopy_sequence (elt);
  507.       XCONS (tail)->car = elt;
  508.  
  509.       for (i = 0; i < XVECTOR (elt)->size; i++)
  510.         if (XTYPE (XVECTOR (elt)->contents[i]) != Lisp_Symbol
  511.         && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
  512.           XVECTOR (elt)->contents[i] =
  513.         Fcopy_keymap (XVECTOR (elt)->contents[i]);
  514.     }
  515.       else if (CONSP (elt)
  516.            && XTYPE (XCONS (elt)->cdr) != Lisp_Symbol
  517.            && ! NILP (Fkeymapp (XCONS (elt)->cdr)))
  518.     XCONS (elt)->cdr = Fcopy_keymap (XCONS (elt)->cdr);
  519.     }
  520.  
  521.   return copy;
  522. }
  523.  
  524. /* Simple Keymap mutators and accessors.                */
  525.  
  526. DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
  527.   "Args KEYMAP, KEY, DEF.  Define key sequence KEY, in KEYMAP, as DEF.\n\
  528. KEYMAP is a keymap.  KEY is a string or a vector of symbols and characters\n\
  529. meaning a sequence of keystrokes and events.\n\
  530. DEF is anything that can be a key's definition:\n\
  531.  nil (means key is undefined in this keymap),\n\
  532.  a command (a Lisp function suitable for interactive calling)\n\
  533.  a string (treated as a keyboard macro),\n\
  534.  a keymap (to define a prefix key),\n\
  535.  a symbol.  When the key is looked up, the symbol will stand for its\n\
  536.     function definition, which should at that time be one of the above,\n\
  537.     or another symbol whose function definition is used, etc.\n\
  538.  a cons (STRING . DEFN), meaning that DEFN is the definition\n\
  539.     (DEFN should be a valid definition in its own right),\n\
  540.  or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
  541. \n\
  542. If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
  543. the front of KEYMAP.")
  544.   (keymap, key, def)
  545.      Lisp_Object keymap;
  546.      Lisp_Object key;
  547.      Lisp_Object def;
  548. {
  549.   register int idx;
  550.   register Lisp_Object c;
  551.   register Lisp_Object cmd;
  552.   int metized = 0;
  553.   int meta_bit;
  554.   int length;
  555.   struct gcpro gcpro1, gcpro2, gcpro3;
  556.  
  557.   keymap = get_keymap (keymap);
  558.  
  559.   if (XTYPE (key) != Lisp_Vector
  560.       && XTYPE (key) != Lisp_String)
  561.     key = wrong_type_argument (Qarrayp, key);
  562.  
  563.   length = XFASTINT (Flength (key));
  564.   if (length == 0)
  565.     return Qnil;
  566.  
  567.   GCPRO3 (keymap, key, def);
  568.  
  569.   if (XTYPE (key) == Lisp_Vector)
  570.     meta_bit = meta_modifier;
  571.   else
  572.     meta_bit = 0x80;
  573.  
  574.   idx = 0;
  575.   while (1)
  576.     {
  577.       c = Faref (key, make_number (idx));
  578.  
  579.       if (XTYPE (c) == Lisp_Int
  580.       && (XINT (c) & meta_bit)
  581.       && !metized)
  582.     {
  583.       c = meta_prefix_char;
  584.       metized = 1;
  585.     }
  586.       else
  587.     {
  588.       if (XTYPE (c) == Lisp_Int)
  589.         XSETINT (c, XINT (c) & ~meta_bit);
  590.  
  591.       metized = 0;
  592.       idx++;
  593.     }
  594.  
  595.       if (idx == length)
  596.     RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
  597.  
  598.       cmd = get_keyelt (access_keymap (keymap, c, 0, 1));
  599.  
  600.       /* If this key is undefined, make it a prefix.  */
  601.       if (NILP (cmd))
  602.     cmd = define_as_prefix (keymap, c);
  603.  
  604.       keymap = get_keymap_1 (cmd, 0, 1);
  605.       if (NILP (keymap))
  606.     {
  607.       /* We must use Fkey_description rather than just passing key to
  608.          error; key might be a vector, not a string.  */
  609.       Lisp_Object description = Fkey_description (key);
  610.  
  611.       error ("Key sequence %s uses invalid prefix characters",
  612.          XSTRING (description)->data);
  613.     }
  614.     }
  615. }
  616.  
  617. /* Value is number if KEY is too long; NIL if valid but has no definition. */
  618.  
  619. DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
  620.   "In keymap KEYMAP, look up key sequence KEY.  Return the definition.\n\
  621. nil means undefined.  See doc of `define-key' for kinds of definitions.\n\
  622. \n\
  623. A number as value means KEY is \"too long\";\n\
  624. that is, characters or symbols in it except for the last one\n\
  625. fail to be a valid sequence of prefix characters in KEYMAP.\n\
  626. The number is how many characters at the front of KEY\n\
  627. it takes to reach a non-prefix command.\n\
  628. \n\
  629. Normally, `lookup-key' ignores bindings for t, which act as default\n\
  630. bindings, used when nothing else in the keymap applies; this makes it\n\
  631. useable as a general function for probing keymaps.  However, if the\n\
  632. third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
  633. recognize the default bindings, just as `read-key-sequence' does.")
  634.   (keymap, key, accept_default)
  635.      register Lisp_Object keymap;
  636.      Lisp_Object key;
  637.      Lisp_Object accept_default;
  638. {
  639.   register int idx;
  640.   register Lisp_Object cmd;
  641.   register Lisp_Object c;
  642.   int metized = 0;
  643.   int length;
  644.   int t_ok = ! NILP (accept_default);
  645.   int meta_bit;
  646.  
  647.   keymap = get_keymap (keymap);
  648.  
  649.   if (XTYPE (key) != Lisp_Vector
  650.       && XTYPE (key) != Lisp_String)
  651.     key = wrong_type_argument (Qarrayp, key);
  652.  
  653.   length = XFASTINT (Flength (key));
  654.   if (length == 0)
  655.     return keymap;
  656.  
  657.   if (XTYPE (key) == Lisp_Vector)
  658.     meta_bit = meta_modifier;
  659.   else
  660.     meta_bit = 0x80;
  661.  
  662.   idx = 0;
  663.   while (1)
  664.     {
  665.       c = Faref (key, make_number (idx));
  666.  
  667.       if (XTYPE (c) == Lisp_Int
  668.       && (XINT (c) & meta_bit)
  669.       && !metized)
  670.     {
  671.       c = meta_prefix_char;
  672.       metized = 1;
  673.     }
  674.       else
  675.     {
  676.       if (XTYPE (c) == Lisp_Int)
  677.         XSETINT (c, XINT (c) & ~meta_bit);
  678.  
  679.       metized = 0;
  680.       idx++;
  681.     }
  682.  
  683.       cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0));
  684.       if (idx == length)
  685.     return cmd;
  686.  
  687.       keymap = get_keymap_1 (cmd, 0, 0);
  688.       if (NILP (keymap))
  689.     return make_number (idx);
  690.  
  691.       QUIT;
  692.     }
  693. }
  694.  
  695. /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
  696.    Assume that currently it does not define C at all.
  697.    Return the keymap.  */
  698.  
  699. static Lisp_Object
  700. define_as_prefix (keymap, c)
  701.      Lisp_Object keymap, c;
  702. {
  703.   Lisp_Object inherit, cmd;
  704.  
  705.   cmd = Fmake_sparse_keymap (Qnil);
  706.   /* If this key is defined as a prefix in an inherited keymap,
  707.      make it a prefix in this map, and make its definition
  708.      inherit the other prefix definition.  */
  709.   inherit = access_keymap (keymap, c, 0, 0);
  710.   if (NILP (inherit))
  711.     {
  712.       /* If there's an inherited keymap
  713.      and it doesn't define this key,
  714.      make it define this key.  */
  715.       Lisp_Object tail;
  716.  
  717.       for (tail = Fcdr (keymap); CONSP (tail); tail = XCONS (tail)->cdr)
  718.     if (EQ (XCONS (tail)->car, Qkeymap))
  719.       break;
  720.  
  721.       if (!NILP (tail))
  722.     inherit = define_as_prefix (tail, c);
  723.     }
  724.  
  725.   cmd = nconc2 (cmd, inherit);
  726.   store_in_keymap (keymap, c, cmd);
  727.  
  728.   return cmd;
  729. }
  730.  
  731. /* Append a key to the end of a key sequence.  We always make a vector.  */
  732.  
  733. Lisp_Object
  734. append_key (key_sequence, key)
  735.      Lisp_Object key_sequence, key;
  736. {
  737.   Lisp_Object args[2];
  738.  
  739.   args[0] = key_sequence;
  740.  
  741.   args[1] = Fcons (key, Qnil);
  742.   return Fvconcat (2, args);
  743. }
  744.  
  745.  
  746. /* Global, local, and minor mode keymap stuff.                */
  747.  
  748. /* We can't put these variables inside current_minor_maps, since under
  749.    some systems, static gets macro-defined to be the empty string.
  750.    Ickypoo.  */
  751. static Lisp_Object *cmm_modes, *cmm_maps;
  752. static int cmm_size;
  753.  
  754. /* Store a pointer to an array of the keymaps of the currently active
  755.    minor modes in *buf, and return the number of maps it contains.
  756.  
  757.    This function always returns a pointer to the same buffer, and may
  758.    free or reallocate it, so if you want to keep it for a long time or
  759.    hand it out to lisp code, copy it.  This procedure will be called
  760.    for every key sequence read, so the nice lispy approach (return a
  761.    new assoclist, list, what have you) for each invocation would
  762.    result in a lot of consing over time.
  763.  
  764.    If we used xrealloc/xmalloc and ran out of memory, they would throw
  765.    back to the command loop, which would try to read a key sequence,
  766.    which would call this function again, resulting in an infinite
  767.    loop.  Instead, we'll use realloc/malloc and silently truncate the
  768.    list, let the key sequence be read, and hope some other piece of
  769.    code signals the error.  */
  770. int
  771. current_minor_maps (modeptr, mapptr)
  772.      Lisp_Object **modeptr, **mapptr;
  773. {
  774.   int i = 0;
  775.   Lisp_Object alist, assoc, var, val;
  776.  
  777.   for (alist = Vminor_mode_map_alist;
  778.        CONSP (alist);
  779.        alist = XCONS (alist)->cdr)
  780.     if (CONSP (assoc = XCONS (alist)->car)
  781.     && XTYPE (var = XCONS (assoc)->car) == Lisp_Symbol
  782.     && ! EQ ((val = find_symbol_value (var)), Qunbound)
  783.     && ! NILP (val))
  784.       {
  785.     if (i >= cmm_size)
  786.       {
  787.         Lisp_Object *newmodes, *newmaps;
  788.  
  789.         if (cmm_maps)
  790.           {
  791.         BLOCK_INPUT;
  792.         newmodes = (Lisp_Object *) realloc (cmm_modes, cmm_size *= 2);
  793.         newmaps  = (Lisp_Object *) realloc (cmm_maps,  cmm_size);
  794.         UNBLOCK_INPUT;
  795.           }
  796.         else
  797.           {
  798.         BLOCK_INPUT;
  799.         newmodes = (Lisp_Object *) malloc (cmm_size = 30);
  800.         newmaps  = (Lisp_Object *) malloc (cmm_size);
  801.         UNBLOCK_INPUT;
  802.           }
  803.  
  804.         if (newmaps && newmodes)
  805.           {
  806.         cmm_modes = newmodes;
  807.         cmm_maps = newmaps;
  808.           }
  809.         else
  810.           break;
  811.       }
  812.     cmm_modes[i] = var;
  813.     cmm_maps [i] = Findirect_function (XCONS (assoc)->cdr);
  814.     i++;
  815.       }
  816.  
  817.   if (modeptr) *modeptr = cmm_modes;
  818.   if (mapptr)  *mapptr  = cmm_maps;
  819.   return i;
  820. }
  821.  
  822. DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
  823.   "Return the binding for command KEY in current keymaps.\n\
  824. KEY is a string or vector, a sequence of keystrokes.\n\
  825. The binding is probably a symbol with a function definition.\n\
  826. \n\
  827. Normally, `key-binding' ignores bindings for t, which act as default\n\
  828. bindings, used when nothing else in the keymap applies; this makes it\n\
  829. useable as a general function for probing keymaps.  However, if the\n\
  830. third optional argument ACCEPT-DEFAULT is non-nil, `key-binding' will\n\
  831. recognize the default bindings, just as `read-key-sequence' does.")
  832.   (key, accept_default)
  833.      Lisp_Object key, accept_default;
  834. {
  835.   Lisp_Object *maps, value;
  836.   int nmaps, i;
  837.  
  838.   nmaps = current_minor_maps (0, &maps);
  839.   for (i = 0; i < nmaps; i++)
  840.     if (! NILP (maps[i]))
  841.       {
  842.     value = Flookup_key (maps[i], key, accept_default);
  843.     if (! NILP (value) && XTYPE (value) != Lisp_Int)
  844.       return value;
  845.       }
  846.  
  847.   if (! NILP (current_buffer->keymap))
  848.     {
  849.       value = Flookup_key (current_buffer->keymap, key, accept_default);
  850.       if (! NILP (value) && XTYPE (value) != Lisp_Int)
  851.     return value;
  852.     }
  853.  
  854.   value = Flookup_key (current_global_map, key, accept_default);
  855.   if (! NILP (value) && XTYPE (value) != Lisp_Int)
  856.     return value;
  857.   
  858.   return Qnil;
  859. }
  860.  
  861. DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
  862.   "Return the binding for command KEYS in current local keymap only.\n\
  863. KEYS is a string, a sequence of keystrokes.\n\
  864. The binding is probably a symbol with a function definition.\n\
  865. \n\
  866. If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
  867. bindings; see the description of `lookup-key' for more details about this.")
  868.   (keys, accept_default)
  869.      Lisp_Object keys, accept_default;
  870. {
  871.   register Lisp_Object map;
  872.   map = current_buffer->keymap;
  873.   if (NILP (map))
  874.     return Qnil;
  875.   return Flookup_key (map, keys, accept_default);
  876. }
  877.  
  878. DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
  879.   "Return the binding for command KEYS in current global keymap only.\n\
  880. KEYS is a string, a sequence of keystrokes.\n\
  881. The binding is probably a symbol with a function definition.\n\
  882. This function's return values are the same as those of lookup-key\n\
  883. (which see).\n\
  884. \n\
  885. If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
  886. bindings; see the description of `lookup-key' for more details about this.")
  887.   (keys, accept_default)
  888.      Lisp_Object keys, accept_default;
  889. {
  890.   return Flookup_key (current_global_map, keys, accept_default);
  891. }
  892.  
  893. DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
  894.   "Find the visible minor mode bindings of KEY.\n\
  895. Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
  896. the symbol which names the minor mode binding KEY, and BINDING is\n\
  897. KEY's definition in that mode.  In particular, if KEY has no\n\
  898. minor-mode bindings, return nil.  If the first binding is a\n\
  899. non-prefix, all subsequent bindings will be omitted, since they would\n\
  900. be ignored.  Similarly, the list doesn't include non-prefix bindings\n\
  901. that come after prefix bindings.\n\
  902. \n\
  903. If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
  904. bindings; see the description of `lookup-key' for more details about this.")
  905.   (key, accept_default)
  906.      Lisp_Object key, accept_default;
  907. {
  908.   Lisp_Object *modes, *maps;
  909.   int nmaps;
  910.   Lisp_Object binding;
  911.   int i, j;
  912.  
  913.   nmaps = current_minor_maps (&modes, &maps);
  914.  
  915.   for (i = j = 0; i < nmaps; i++)
  916.     if (! NILP (maps[i])
  917.     && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
  918.     && XTYPE (binding) != Lisp_Int)
  919.       {
  920.     if (! NILP (get_keymap (binding)))
  921.       maps[j++] = Fcons (modes[i], binding);
  922.     else if (j == 0)
  923.       return Fcons (Fcons (modes[i], binding), Qnil);
  924.       }
  925.  
  926.   return Flist (j, maps);
  927. }
  928.  
  929. DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2,
  930.   "kSet key globally: \nCSet key %s to command: ",
  931.   "Give KEY a global binding as COMMAND.\n\
  932. COMMAND is a symbol naming an interactively-callable function.\n\
  933. KEY is a key sequence (a string or vector of characters or event types).\n\
  934. Note that if KEY has a local binding in the current buffer\n\
  935. that local binding will continue to shadow any global binding.")
  936.   (keys, function)
  937.      Lisp_Object keys, function;
  938. {
  939.   if (XTYPE (keys) != Lisp_Vector
  940.       && XTYPE (keys) != Lisp_String)
  941.     keys = wrong_type_argument (Qarrayp, keys);
  942.  
  943.   Fdefine_key (current_global_map, keys, function);
  944.   return Qnil;
  945. }
  946.  
  947. DEFUN ("local-set-key", Flocal_set_key, Slocal_set_key, 2, 2,
  948.   "kSet key locally: \nCSet key %s locally to command: ",
  949.   "Give KEY a local binding as COMMAND.\n\
  950. COMMAND is a symbol naming an interactively-callable function.\n\
  951. KEY is a key sequence (a string or vector of characters or event types).\n\
  952. The binding goes in the current buffer's local map,\n\
  953. which is shared with other buffers in the same major mode.")
  954.   (keys, function)
  955.      Lisp_Object keys, function;
  956. {
  957.   register Lisp_Object map;
  958.   map = current_buffer->keymap;
  959.   if (NILP (map))
  960.     {
  961.       map = Fmake_sparse_keymap (Qnil);
  962.       current_buffer->keymap = map;
  963.     }
  964.  
  965.   if (XTYPE (keys) != Lisp_Vector
  966.       && XTYPE (keys) != Lisp_String)
  967.     keys = wrong_type_argument (Qarrayp, keys);
  968.  
  969.   Fdefine_key (map, keys, function);
  970.   return Qnil;
  971. }
  972.  
  973. DEFUN ("global-unset-key", Fglobal_unset_key, Sglobal_unset_key,
  974.   1, 1, "kUnset key globally: ",
  975.   "Remove global binding of KEY.\n\
  976. KEY is a string representing a sequence of keystrokes.")
  977.   (keys)
  978.      Lisp_Object keys;
  979. {
  980.   return Fglobal_set_key (keys, Qnil);
  981. }
  982.  
  983. DEFUN ("local-unset-key", Flocal_unset_key, Slocal_unset_key, 1, 1,
  984.   "kUnset key locally: ",
  985.   "Remove local binding of KEY.\n\
  986. KEY is a string representing a sequence of keystrokes.")
  987.   (keys)
  988.      Lisp_Object keys;
  989. {
  990.   if (!NILP (current_buffer->keymap))
  991.     Flocal_set_key (keys, Qnil);
  992.   return Qnil;
  993. }
  994.  
  995. DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 2, 0,
  996.   "Define COMMAND as a prefix command.  COMMAND should be a symbol.\n\
  997. A new sparse keymap is stored as COMMAND's function definition and its value.\n\
  998. If a second optional argument MAPVAR is given, the map is stored as\n\
  999. its value instead of as COMMAND's value; but COMMAND is still defined\n\
  1000. as a function.")
  1001.   (name, mapvar)
  1002.      Lisp_Object name, mapvar;
  1003. {
  1004.   Lisp_Object map;
  1005.   map = Fmake_sparse_keymap (Qnil);
  1006.   Ffset (name, map);
  1007.   if (!NILP (mapvar))
  1008.     Fset (mapvar, map);
  1009.   else
  1010.     Fset (name, map);
  1011.   return name;
  1012. }
  1013.  
  1014. DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
  1015.   "Select KEYMAP as the global keymap.")
  1016.   (keymap)
  1017.      Lisp_Object keymap;
  1018. {
  1019.   keymap = get_keymap (keymap);
  1020.   current_global_map = keymap;
  1021.   return Qnil;
  1022. }
  1023.  
  1024. DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
  1025.   "Select KEYMAP as the local keymap.\n\
  1026. If KEYMAP is nil, that means no local keymap.")
  1027.   (keymap)
  1028.      Lisp_Object keymap;
  1029. {
  1030.   if (!NILP (keymap))
  1031.     keymap = get_keymap (keymap);
  1032.  
  1033.   current_buffer->keymap = keymap;
  1034.  
  1035.   return Qnil;
  1036. }
  1037.  
  1038. DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
  1039.   "Return current buffer's local keymap, or nil if it has none.")
  1040.   ()
  1041. {
  1042.   return current_buffer->keymap;
  1043. }
  1044.  
  1045. DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
  1046.   "Return the current global keymap.")
  1047.   ()
  1048. {
  1049.   return current_global_map;
  1050. }
  1051.  
  1052. DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
  1053.   "Return a list of keymaps for the minor modes of the current buffer.")
  1054.   ()
  1055. {
  1056.   Lisp_Object *maps;
  1057.   int nmaps = current_minor_maps (0, &maps);
  1058.  
  1059.   return Flist (nmaps, maps);
  1060. }
  1061.  
  1062. /* Help functions for describing and documenting keymaps.        */
  1063.  
  1064. DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
  1065.   1, 2, 0,
  1066.   "Find all keymaps accessible via prefix characters from KEYMAP.\n\
  1067. Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
  1068. KEYS starting from KEYMAP gets you to MAP.  These elements are ordered\n\
  1069. so that the KEYS increase in length.  The first element is (\"\" . KEYMAP).\n\
  1070. An optional argument PREFIX, if non-nil, should be a key sequence;\n\
  1071. then the value includes only maps for prefixes that start with PREFIX.")
  1072.   (startmap, prefix)
  1073.      Lisp_Object startmap, prefix;
  1074. {
  1075.   Lisp_Object maps, good_maps, tail;
  1076.   int prefixlen = 0;
  1077.  
  1078.   if (!NILP (prefix))
  1079.     prefixlen = XINT (Flength (prefix));
  1080.  
  1081.   maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
  1082.                get_keymap (startmap)),
  1083.         Qnil);
  1084.  
  1085.   /* For each map in the list maps,
  1086.      look at any other maps it points to,
  1087.      and stick them at the end if they are not already in the list.
  1088.  
  1089.      This is a breadth-first traversal, where tail is the queue of
  1090.      nodes, and maps accumulates a list of all nodes visited.  */
  1091.  
  1092.   for (tail = maps; CONSP (tail); tail = XCONS (tail)->cdr)
  1093.     {
  1094.       register Lisp_Object thisseq = Fcar (Fcar (tail));
  1095.       register Lisp_Object thismap = Fcdr (Fcar (tail));
  1096.       Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
  1097.  
  1098.       /* Does the current sequence end in the meta-prefix-char?  */
  1099.       int is_metized = (XINT (last) >= 0
  1100.             && EQ (Faref (thisseq, last), meta_prefix_char));
  1101.  
  1102.       for (; CONSP (thismap); thismap = XCONS (thismap)->cdr)
  1103.     {
  1104.       Lisp_Object elt = XCONS (thismap)->car;
  1105.  
  1106.       QUIT;
  1107.  
  1108.       if (XTYPE (elt) == Lisp_Vector)
  1109.         {
  1110.           register int i;
  1111.  
  1112.           /* Vector keymap.  Scan all the elements.  */
  1113.           for (i = 0; i < XVECTOR (elt)->size; i++)
  1114.         {
  1115.           register Lisp_Object tem;
  1116.           register Lisp_Object cmd;
  1117.  
  1118.           cmd = get_keyelt (XVECTOR (elt)->contents[i]);
  1119.           if (NILP (cmd)) continue;
  1120.           tem = Fkeymapp (cmd);
  1121.           if (!NILP (tem))
  1122.             {
  1123.               cmd = get_keymap (cmd);
  1124.               /* Ignore keymaps that are already added to maps.  */
  1125.               tem = Frassq (cmd, maps);
  1126.               if (NILP (tem))
  1127.             {
  1128.               /* If the last key in thisseq is meta-prefix-char,
  1129.                  turn it into a meta-ized keystroke.  We know
  1130.                  that the event we're about to append is an
  1131.                  ascii keystroke since we're processing a
  1132.                  keymap table.  */
  1133.               if (is_metized)
  1134.                 {
  1135.                   int meta_bit = meta_modifier;
  1136.                   tem = Fcopy_sequence (thisseq);
  1137.                   
  1138.                   Faset (tem, last, make_number (i | meta_bit));
  1139.                   
  1140.                   /* This new sequence is the same length as
  1141.                  thisseq, so stick it in the list right
  1142.                  after this one.  */
  1143.                   XCONS (tail)->cdr
  1144.                 = Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
  1145.                 }
  1146.               else
  1147.                 {
  1148.                   tem = append_key (thisseq, make_number (i));
  1149.                   nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
  1150.                 }
  1151.             }
  1152.             }
  1153.         }
  1154.         }        
  1155.       else if (CONSP (elt))
  1156.         {
  1157.           register Lisp_Object cmd = get_keyelt (XCONS (elt)->cdr);
  1158.           register Lisp_Object tem;
  1159.  
  1160.           /* Ignore definitions that aren't keymaps themselves.  */
  1161.           tem = Fkeymapp (cmd);
  1162.           if (!NILP (tem))
  1163.         {
  1164.           /* Ignore keymaps that have been seen already.  */
  1165.           cmd = get_keymap (cmd);
  1166.           tem = Frassq (cmd, maps);
  1167.           if (NILP (tem))
  1168.             {
  1169.               /* Let elt be the event defined by this map entry.  */
  1170.               elt = XCONS (elt)->car;
  1171.  
  1172.               /* If the last key in thisseq is meta-prefix-char, and
  1173.              this entry is a binding for an ascii keystroke,
  1174.              turn it into a meta-ized keystroke.  */
  1175.               if (is_metized && XTYPE (elt) == Lisp_Int)
  1176.             {
  1177.               tem = Fcopy_sequence (thisseq);
  1178.               Faset (tem, last,
  1179.                  make_number (XINT (elt) | meta_modifier));
  1180.  
  1181.               /* This new sequence is the same length as
  1182.                  thisseq, so stick it in the list right
  1183.                  after this one.  */
  1184.               XCONS (tail)->cdr
  1185.                 = Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
  1186.             }
  1187.               else
  1188.             nconc2 (tail,
  1189.                 Fcons (Fcons (append_key (thisseq, elt), cmd),
  1190.                        Qnil));
  1191.             }
  1192.         }
  1193.         }
  1194.     }
  1195.     }
  1196.  
  1197.   if (NILP (prefix))
  1198.     return maps;
  1199.  
  1200.   /* Now find just the maps whose access prefixes start with PREFIX.  */
  1201.  
  1202.   good_maps = Qnil;
  1203.   for (; CONSP (maps); maps = XCONS (maps)->cdr)
  1204.     {
  1205.       Lisp_Object elt, thisseq;
  1206.       elt = XCONS (maps)->car;
  1207.       thisseq = XCONS (elt)->car;
  1208.       /* The access prefix must be at least as long as PREFIX,
  1209.      and the first elements must match those of PREFIX.  */
  1210.       if (XINT (Flength (thisseq)) >= prefixlen)
  1211.     {
  1212.       int i;
  1213.       for (i = 0; i < prefixlen; i++)
  1214.         {
  1215.           Lisp_Object i1;
  1216.           XFASTINT (i1) = i;
  1217.           if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
  1218.         break;
  1219.         }
  1220.       if (i == prefixlen)
  1221.         good_maps = Fcons (elt, good_maps);
  1222.     }
  1223.     }
  1224.  
  1225.   return Fnreverse (good_maps);
  1226. }
  1227.  
  1228. Lisp_Object Qsingle_key_description, Qkey_description;
  1229.  
  1230. DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
  1231.   "Return a pretty description of key-sequence KEYS.\n\
  1232. Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
  1233. spaces are put between sequence elements, etc.")
  1234.   (keys)
  1235.      Lisp_Object keys;
  1236. {
  1237.   if (XTYPE (keys) == Lisp_String)
  1238.     {
  1239.       Lisp_Object vector;
  1240.       int i;
  1241.       vector = Fmake_vector (Flength (keys), Qnil);
  1242.       for (i = 0; i < XSTRING (keys)->size; i++)
  1243.     {
  1244.       if (XSTRING (keys)->data[i] & 0x80)
  1245.         XFASTINT (XVECTOR (vector)->contents[i])
  1246.           = meta_modifier | (XSTRING (keys)->data[i] & ~0x80);
  1247.       else
  1248.         XFASTINT (XVECTOR (vector)->contents[i])
  1249.           = XSTRING (keys)->data[i];
  1250.     }
  1251.       keys = vector;
  1252.     }
  1253.   return Fmapconcat (Qsingle_key_description, keys, build_string (" "));
  1254. }
  1255.  
  1256. char *
  1257. push_key_description (c, p)
  1258.      register unsigned int c;
  1259.      register char *p;
  1260. {
  1261.   /* Clear all the meaningless bits above the meta bit.  */
  1262.   c &= meta_modifier | ~ - meta_modifier;
  1263.  
  1264.   if (c & alt_modifier)
  1265.     {
  1266.       *p++ = 'A';
  1267.       *p++ = '-';
  1268.       c -= alt_modifier;
  1269.     }
  1270.   if (c & ctrl_modifier)
  1271.     {
  1272.       *p++ = 'C';
  1273.       *p++ = '-';
  1274.       c -= ctrl_modifier;
  1275.     }
  1276.   if (c & hyper_modifier)
  1277.     {
  1278.       *p++ = 'H';
  1279.       *p++ = '-';
  1280.       c -= hyper_modifier;
  1281.     }
  1282.   if (c & meta_modifier)
  1283.     {
  1284.       *p++ = 'M';
  1285.       *p++ = '-';
  1286.       c -= meta_modifier;
  1287.     }
  1288.   if (c & shift_modifier)
  1289.     {
  1290.       *p++ = 'S';
  1291.       *p++ = '-';
  1292.       c -= shift_modifier;
  1293.     }
  1294.   if (c & super_modifier)
  1295.     {
  1296.       *p++ = 's';
  1297.       *p++ = '-';
  1298.       c -= super_modifier;
  1299.     }
  1300.   if (c < 040)
  1301.     {
  1302.       if (c == 033)
  1303.     {
  1304.       *p++ = 'E';
  1305.       *p++ = 'S';
  1306.       *p++ = 'C';
  1307.     }
  1308.       else if (c == '\t')
  1309.     {
  1310.       *p++ = 'T';
  1311.       *p++ = 'A';
  1312.       *p++ = 'B';
  1313.     }
  1314.       else if (c == Ctl('J'))
  1315.     {
  1316.       *p++ = 'L';
  1317.       *p++ = 'F';
  1318.       *p++ = 'D';
  1319.     }
  1320.       else if (c == Ctl('M'))
  1321.     {
  1322.       *p++ = 'R';
  1323.       *p++ = 'E';
  1324.       *p++ = 'T';
  1325.     }
  1326.       else
  1327.     {
  1328.       *p++ = 'C';
  1329.       *p++ = '-';
  1330.       if (c > 0 && c <= Ctl ('Z'))
  1331.         *p++ = c + 0140;
  1332.       else
  1333.         *p++ = c + 0100;
  1334.     }
  1335.     }
  1336.   else if (c == 0177)
  1337.     {
  1338.       *p++ = 'D';
  1339.       *p++ = 'E';
  1340.       *p++ = 'L';
  1341.     }
  1342.   else if (c == ' ')
  1343.     {
  1344.       *p++ = 'S';
  1345.       *p++ = 'P';
  1346.       *p++ = 'C';
  1347.     }
  1348.   else if (c < 256)
  1349.     *p++ = c;
  1350.   else
  1351.     {
  1352.       *p++ = '\\';
  1353.       *p++ = (7 & (c >> 15)) + '0';
  1354.       *p++ = (7 & (c >> 12)) + '0';
  1355.       *p++ = (7 & (c >> 9)) + '0';
  1356.       *p++ = (7 & (c >> 6)) + '0';
  1357.       *p++ = (7 & (c >> 3)) + '0';
  1358.       *p++ = (7 & (c >> 0)) + '0';
  1359.     }
  1360.  
  1361.   return p;  
  1362. }
  1363.  
  1364. DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
  1365.   "Return a pretty description of command character KEY.\n\
  1366. Control characters turn into C-whatever, etc.")
  1367.   (key)
  1368.      Lisp_Object key;
  1369. {
  1370.   char tem[20];
  1371.  
  1372.   key = EVENT_HEAD (key);
  1373.  
  1374.   switch (XTYPE (key))
  1375.     {
  1376.     case Lisp_Int:        /* Normal character */
  1377.       *push_key_description (XUINT (key), tem) = 0;
  1378.       return build_string (tem);
  1379.  
  1380.     case Lisp_Symbol:        /* Function key or event-symbol */
  1381.       return Fsymbol_name (key);
  1382.  
  1383.     default:
  1384.       error ("KEY must be an integer, cons, or symbol.");
  1385.     }
  1386. }
  1387.  
  1388. char *
  1389. push_text_char_description (c, p)
  1390.      register unsigned int c;
  1391.      register char *p;
  1392. {
  1393.   if (c >= 0200)
  1394.     {
  1395.       *p++ = 'M';
  1396.       *p++ = '-';
  1397.       c -= 0200;
  1398.     }
  1399.   if (c < 040)
  1400.     {
  1401.       *p++ = '^';
  1402.       *p++ = c + 64;        /* 'A' - 1 */
  1403.     }
  1404.   else if (c == 0177)
  1405.     {
  1406.       *p++ = '^';
  1407.       *p++ = '?';
  1408.     }
  1409.   else
  1410.     *p++ = c;
  1411.   return p;  
  1412. }
  1413.  
  1414. DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
  1415.   "Return a pretty description of file-character CHAR.\n\
  1416. Control characters turn into \"^char\", etc.")
  1417.   (chr)
  1418.      Lisp_Object chr;
  1419. {
  1420.   char tem[6];
  1421.  
  1422.   CHECK_NUMBER (chr, 0);
  1423.  
  1424.   *push_text_char_description (XINT (chr) & 0377, tem) = 0;
  1425.  
  1426.   return build_string (tem);
  1427. }
  1428.  
  1429. /* Return non-zero if SEQ contains only ASCII characters, perhaps with
  1430.    a meta bit.  */
  1431. static int
  1432. ascii_sequence_p (seq)
  1433.      Lisp_Object seq;
  1434. {
  1435.   Lisp_Object i;
  1436.   int len = XINT (Flength (seq));
  1437.   
  1438.   for (XFASTINT (i) = 0; XFASTINT (i) < len; XFASTINT (i)++)
  1439.     {
  1440.       Lisp_Object elt = Faref (seq, i);
  1441.  
  1442.       if (XTYPE (elt) != Lisp_Int
  1443.       || (XUINT (elt) & ~CHAR_META) >= 0x80)
  1444.     return 0;
  1445.     }
  1446.  
  1447.   return 1;
  1448. }
  1449.  
  1450.  
  1451. /* where-is - finding a command in a set of keymaps.            */
  1452.  
  1453. DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
  1454.   "Return list of keys that invoke DEFINITION in KEYMAP or KEYMAP1.\n\
  1455. If KEYMAP is nil, search only KEYMAP1.\n\
  1456. If KEYMAP1 is nil, use the current global map.\n\
  1457. \n\
  1458. If optional 4th arg FIRSTONLY is non-nil, return a string representing\n\
  1459. the first key sequence found, rather than a list of all possible key\n\
  1460. sequences.  If FIRSTONLY is t, avoid key sequences which use non-ASCII\n\
  1461. keys and therefore may not be usable on ASCII terminals.  If FIRSTONLY\n\
  1462. is the symbol `non-ascii', return the first binding found, no matter\n\
  1463. what its components.\n\
  1464. \n\
  1465. If optional 5th arg NOINDIRECT is non-nil, don't follow indirections\n\
  1466. to other keymaps or slots.  This makes it possible to search for an\n\
  1467. indirect definition itself.")
  1468.   (definition, local_keymap, global_keymap, firstonly, noindirect)
  1469.      Lisp_Object definition, local_keymap, global_keymap;
  1470.      Lisp_Object firstonly, noindirect;
  1471. {
  1472.   register Lisp_Object maps;
  1473.   Lisp_Object found;
  1474.  
  1475.   if (NILP (global_keymap))
  1476.     global_keymap = current_global_map;
  1477.  
  1478.   if (!NILP (local_keymap))
  1479.     maps = nconc2 (Faccessible_keymaps (get_keymap (local_keymap), Qnil),
  1480.            Faccessible_keymaps (get_keymap (global_keymap), Qnil));
  1481.   else
  1482.     maps = Faccessible_keymaps (get_keymap (global_keymap), Qnil);
  1483.  
  1484.   found = Qnil;
  1485.  
  1486.   for (; !NILP (maps); maps = Fcdr (maps))
  1487.     {
  1488.       /* Key sequence to reach map */
  1489.       register Lisp_Object this = Fcar (Fcar (maps));
  1490.  
  1491.       /* The map that it reaches */
  1492.       register Lisp_Object map  = Fcdr (Fcar (maps));
  1493.  
  1494.       /* If Fcar (map) is a VECTOR, the current element within that vector.  */
  1495.       int i = 0;
  1496.  
  1497.       /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
  1498.      [M-CHAR] sequences, check if last character of the sequence
  1499.      is the meta-prefix char.  */
  1500.       Lisp_Object last = make_number (XINT (Flength (this)) - 1);
  1501.       int last_is_meta = (XINT (last) >= 0
  1502.               && EQ (Faref (this, last), meta_prefix_char));
  1503.  
  1504.       QUIT;
  1505.  
  1506.       while (CONSP (map))
  1507.     {
  1508.       /* Because the code we want to run on each binding is rather
  1509.          large, we don't want to have two separate loop bodies for
  1510.          sparse keymap bindings and tables; we want to iterate one
  1511.          loop body over both keymap and vector bindings.
  1512.  
  1513.          For this reason, if Fcar (map) is a vector, we don't
  1514.          advance map to the next element until i indicates that we
  1515.          have finished off the vector.  */
  1516.       
  1517.       Lisp_Object elt = XCONS (map)->car;
  1518.       Lisp_Object key, binding, sequence;
  1519.  
  1520.       QUIT;
  1521.  
  1522.       /* Set key and binding to the current key and binding, and
  1523.          advance map and i to the next binding.  */
  1524.       if (XTYPE (elt) == Lisp_Vector)
  1525.         {
  1526.           /* In a vector, look at each element.  */
  1527.           binding = XVECTOR (elt)->contents[i];
  1528.           XFASTINT (key) = i;
  1529.           i++;
  1530.  
  1531.           /* If we've just finished scanning a vector, advance map
  1532.          to the next element, and reset i in anticipation of the
  1533.          next vector we may find.  */
  1534.           if (i >= XVECTOR (elt)->size)
  1535.         {
  1536.           map = XCONS (map)->cdr;
  1537.           i = 0;
  1538.         }
  1539.         }
  1540.       else if (CONSP (elt))
  1541.         {
  1542.           key = Fcar (Fcar (map));
  1543.           binding = Fcdr (Fcar (map));
  1544.  
  1545.           map = XCONS (map)->cdr;
  1546.         }
  1547.       else
  1548.         /* We want to ignore keymap elements that are neither
  1549.            vectors nor conses.  */
  1550.         {
  1551.           map = XCONS (map)->cdr;
  1552.           continue;
  1553.         }
  1554.  
  1555.       /* Search through indirections unless that's not wanted.  */
  1556.       if (NILP (noindirect))
  1557.         binding = get_keyelt (binding);
  1558.  
  1559.       /* End this iteration if this element does not match
  1560.          the target.  */
  1561.  
  1562.       if (XTYPE (definition) == Lisp_Cons)
  1563.         {
  1564.           Lisp_Object tem;
  1565.           tem = Fequal (binding, definition);
  1566.           if (NILP (tem))
  1567.         continue;
  1568.         }
  1569.       else
  1570.         if (!EQ (binding, definition))
  1571.           continue;
  1572.  
  1573.       /* We have found a match.
  1574.          Construct the key sequence where we found it.  */
  1575.       if (XTYPE (key) == Lisp_Int && last_is_meta)
  1576.         {
  1577.           sequence = Fcopy_sequence (this);
  1578.           Faset (sequence, last, make_number (XINT (key) | meta_modifier));
  1579.         }
  1580.       else
  1581.         sequence = append_key (this, key);
  1582.  
  1583.       /* Verify that this key binding is not shadowed by another
  1584.          binding for the same key, before we say it exists.
  1585.  
  1586.          Mechanism: look for local definition of this key and if
  1587.          it is defined and does not match what we found then
  1588.          ignore this key.
  1589.  
  1590.          Either nil or number as value from Flookup_key
  1591.          means undefined.  */
  1592.       if (!NILP (local_keymap))
  1593.         {
  1594.           binding = Flookup_key (local_keymap, sequence, Qnil);
  1595.           if (!NILP (binding) && XTYPE (binding) != Lisp_Int)
  1596.         {
  1597.           if (XTYPE (definition) == Lisp_Cons)
  1598.             {
  1599.               Lisp_Object tem;
  1600.               tem = Fequal (binding, definition);
  1601.               if (NILP (tem))
  1602.             continue;
  1603.             }
  1604.           else
  1605.             if (!EQ (binding, definition))
  1606.               continue;
  1607.         }
  1608.         }
  1609.  
  1610.       /* It is a true unshadowed match.  Record it.  */
  1611.       found = Fcons (sequence, found);
  1612.  
  1613.       /* If firstonly is Qnon_ascii, then we can return the first
  1614.          binding we find.  If firstonly is not Qnon_ascii but not
  1615.          nil, then we should return the first ascii-only binding
  1616.          we find.  */
  1617.       if (EQ (firstonly, Qnon_ascii))
  1618.         return sequence;
  1619.       else if (! NILP (firstonly) && ascii_sequence_p (sequence))
  1620.         return sequence;
  1621.     }
  1622.     }
  1623.  
  1624.   found = Fnreverse (found);
  1625.  
  1626.   /* firstonly may have been t, but we may have gone all the way through
  1627.      the keymaps without finding an all-ASCII key sequence.  So just
  1628.      return the best we could find.  */
  1629.   if (! NILP (firstonly))
  1630.     return Fcar (found);
  1631.     
  1632.   return found;
  1633. }
  1634.  
  1635. /* Return a string listing the keys and buttons that run DEFINITION.  */
  1636.  
  1637. static Lisp_Object
  1638. where_is_string (definition)
  1639.      Lisp_Object definition;
  1640. {
  1641.   register Lisp_Object keys, keys1;
  1642.  
  1643.   keys = Fwhere_is_internal (definition,
  1644.                  current_buffer->keymap, Qnil, Qnil, Qnil);
  1645.   keys1 = Fmapconcat (Qkey_description, keys, build_string (", "));
  1646.  
  1647.   return keys1;
  1648. }
  1649.  
  1650. DEFUN ("where-is", Fwhere_is, Swhere_is, 1, 1, "CWhere is command: ",
  1651.   "Print message listing key sequences that invoke specified command.\n\
  1652. Argument is a command definition, usually a symbol with a function definition.")
  1653.   (definition)
  1654.      Lisp_Object definition;
  1655. {
  1656.   register Lisp_Object string;
  1657.  
  1658.   CHECK_SYMBOL (definition, 0);
  1659.   string = where_is_string (definition);
  1660.  
  1661.   if (XSTRING (string)->size)
  1662.     message ("%s is on %s", XSYMBOL (definition)->name->data,
  1663.          XSTRING (string)->data);
  1664.   else
  1665.     message ("%s is not on any key", XSYMBOL (definition)->name->data);
  1666.   return Qnil;
  1667. }
  1668.  
  1669. /* describe-bindings - summarizing all the bindings in a set of keymaps.  */
  1670.  
  1671. DEFUN ("describe-bindings", Fdescribe_bindings, Sdescribe_bindings, 0, 1, "",
  1672.   "Show a list of all defined keys, and their definitions.\n\
  1673. The list is put in a buffer, which is displayed.\n\
  1674. An optional argument PREFIX, if non-nil, should be a key sequence;\n\
  1675. then we display only bindings that start with that prefix.")
  1676.   (prefix)
  1677.      Lisp_Object prefix;
  1678. {
  1679.   register Lisp_Object thisbuf;
  1680.   XSET (thisbuf, Lisp_Buffer, current_buffer);
  1681.   internal_with_output_to_temp_buffer ("*Help*",
  1682.                        describe_buffer_bindings,
  1683.                        Fcons (thisbuf, prefix));
  1684.   return Qnil;
  1685. }
  1686.  
  1687. /* ARG is (BUFFER . PREFIX).  */
  1688.  
  1689. static Lisp_Object
  1690. describe_buffer_bindings (arg)
  1691.      Lisp_Object arg;
  1692. {
  1693.   Lisp_Object descbuf, prefix, shadow;
  1694.   register Lisp_Object start1;
  1695.  
  1696.   char *alternate_heading
  1697.     = "\
  1698. Alternate Characters (use anywhere the nominal character is listed):\n\
  1699. nominal         alternate\n\
  1700. -------         ---------\n";
  1701.  
  1702.   descbuf = XCONS (arg)->car;
  1703.   prefix = XCONS (arg)->cdr;
  1704.  
  1705.   Fset_buffer (Vstandard_output);
  1706.  
  1707.   /* Report on alternates for keys.  */
  1708.   if (XTYPE (Vkeyboard_translate_table) == Lisp_String)
  1709.     {
  1710.       int c;
  1711.       unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
  1712.       int translate_len = XSTRING (Vkeyboard_translate_table)->size;
  1713.  
  1714.       for (c = 0; c < translate_len; c++)
  1715.     if (translate[c] != c)
  1716.       {
  1717.         char buf[20];
  1718.         char *bufend;
  1719.  
  1720.         if (alternate_heading)
  1721.           {
  1722.         insert_string (alternate_heading);
  1723.         alternate_heading = 0;
  1724.           }
  1725.  
  1726.         bufend = push_key_description (translate[c], buf);
  1727.         insert (buf, bufend - buf);
  1728.         Findent_to (make_number (16), make_number (1));
  1729.         bufend = push_key_description (c, buf);
  1730.         insert (buf, bufend - buf);
  1731.  
  1732.         insert ("\n", 1);
  1733.       }
  1734.  
  1735.       insert ("\n", 1);
  1736.     }
  1737.  
  1738.   {
  1739.     int i, nmaps;
  1740.     Lisp_Object *modes, *maps;
  1741.  
  1742.     shadow = Qnil;
  1743.  
  1744.     /* Temporarily switch to descbuf, so that we can get that buffer's
  1745.        minor modes correctly.  */
  1746.     Fset_buffer (descbuf);
  1747.     nmaps = current_minor_maps (&modes, &maps);
  1748.     Fset_buffer (Vstandard_output);
  1749.  
  1750.     shadow = Qnil;
  1751.  
  1752.     /* Print the minor mode maps.  */
  1753.     for (i = 0; i < nmaps; i++)
  1754.       {
  1755.     /* Tht title for a minor mode keymap
  1756.        is constructed at run time.
  1757.        We let describe_map_tree do the actual insertion
  1758.        because it takes care of other features when doing so.  */
  1759.     char *title = (char *) alloca (40 + XSYMBOL (modes[i])->name->size);
  1760.     char *p = title;
  1761.  
  1762.     if (XTYPE (modes[i]) == Lisp_Symbol)
  1763.       {
  1764.         *p++ = '`';
  1765.         bcopy (XSYMBOL (modes[i])->name->data, p,
  1766.            XSYMBOL (modes[i])->name->size);
  1767.         p += XSYMBOL (modes[i])->name->size;
  1768.         *p++ = '\'';
  1769.       }
  1770.     else
  1771.       {
  1772.         bcopy ("Strangely Named", p, sizeof ("Strangely Named"));
  1773.         p += sizeof ("Strangely Named");
  1774.       }
  1775.     bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings"));
  1776.     p += sizeof (" Minor Mode Bindings");
  1777.     *p = 0;
  1778.  
  1779.     describe_map_tree (maps[i], 0, shadow, prefix, title);
  1780.     shadow = Fcons (maps[i], shadow);
  1781.       }
  1782.   }
  1783.  
  1784.   /* Print the (major mode) local map.  */
  1785.   start1 = XBUFFER (descbuf)->keymap;
  1786.   if (!NILP (start1))
  1787.     {
  1788.       describe_map_tree (start1, 0, shadow, prefix,
  1789.              "Major Mode Bindings");
  1790.       shadow = Fcons (start1, shadow);
  1791.     }
  1792.  
  1793.   describe_map_tree (current_global_map, 0, shadow, prefix,
  1794.              "Global Bindings");
  1795.  
  1796.   Fset_buffer (descbuf);
  1797.   return Qnil;
  1798. }
  1799.  
  1800. /* Insert a desription of the key bindings in STARTMAP,
  1801.     followed by those of all maps reachable through STARTMAP.
  1802.    If PARTIAL is nonzero, omit certain "uninteresting" commands
  1803.     (such as `undefined').
  1804.    If SHADOW is non-nil, it is a list of maps;
  1805.     don't mention keys which would be shadowed by any of them.
  1806.    PREFIX, if non-nil, says mention only keys that start with PREFIX.
  1807.    TITLE, if not 0, is a string to insert at the beginning.
  1808.    TITLE should not end with a colon or a newline; we supply that.  */
  1809.  
  1810. void
  1811. describe_map_tree (startmap, partial, shadow, prefix, title)
  1812.      Lisp_Object startmap, shadow, prefix;
  1813.      int partial;
  1814.      char *title;
  1815. {
  1816.   Lisp_Object maps;
  1817.   struct gcpro gcpro1;
  1818.   int something = 0;
  1819.   char *key_heading
  1820.     = "\
  1821. key             binding\n\
  1822. ---             -------\n";
  1823.  
  1824.   maps = Faccessible_keymaps (startmap, prefix);
  1825.   GCPRO1 (maps);
  1826.  
  1827.   if (!NILP (maps))
  1828.     {
  1829.       if (title)
  1830.     {
  1831.       insert_string (title);
  1832.       if (!NILP (prefix))
  1833.         {
  1834.           insert_string (" Starting With ");
  1835.           insert1 (Fkey_description (prefix));
  1836.         }
  1837.       insert_string (":\n");
  1838.     }
  1839.       insert_string (key_heading);
  1840.       something = 1;
  1841.     }
  1842.  
  1843.   for (; !NILP (maps); maps = Fcdr (maps))
  1844.     {
  1845.       register Lisp_Object elt, prefix, sub_shadows, tail;
  1846.  
  1847.       elt = Fcar (maps);
  1848.       prefix = Fcar (elt);
  1849.  
  1850.       sub_shadows = Qnil;
  1851.  
  1852.       for (tail = shadow; CONSP (tail); tail = XCONS (tail)->cdr)
  1853.     {
  1854.       Lisp_Object shmap;
  1855.  
  1856.       shmap = XCONS (tail)->car;
  1857.  
  1858.       /* If the sequence by which we reach this keymap is zero-length,
  1859.          then the shadow map for this keymap is just SHADOW.  */
  1860.       if ((XTYPE (prefix) == Lisp_String
  1861.            && XSTRING (prefix)->size == 0)
  1862.           || (XTYPE (prefix) == Lisp_Vector
  1863.           && XVECTOR (prefix)->size == 0))
  1864.         ;
  1865.       /* If the sequence by which we reach this keymap actually has
  1866.          some elements, then the sequence's definition in SHADOW is
  1867.          what we should use.  */
  1868.       else
  1869.         {
  1870.           shmap = Flookup_key (shadow, Fcar (elt), Qt);
  1871.           if (XTYPE (shmap) == Lisp_Int)
  1872.         shmap = Qnil;
  1873.         }
  1874.  
  1875.       /* If shmap is not nil and not a keymap,
  1876.          it completely shadows this map, so don't
  1877.          describe this map at all.  */
  1878.       if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
  1879.         goto skip;
  1880.  
  1881.       if (!NILP (shmap))
  1882.         sub_shadows = Fcons (shmap, sub_shadows);
  1883.     }
  1884.  
  1885.       describe_map (Fcdr (elt), Fcar (elt), partial, sub_shadows);
  1886.  
  1887.     skip: ;
  1888.     }
  1889.  
  1890.   if (something)
  1891.     insert_string ("\n");
  1892.  
  1893.   UNGCPRO;
  1894. }
  1895.  
  1896. static void
  1897. describe_command (definition)
  1898.      Lisp_Object definition;
  1899. {
  1900.   register Lisp_Object tem1;
  1901.  
  1902.   Findent_to (make_number (16), make_number (1));
  1903.  
  1904.   if (XTYPE (definition) == Lisp_Symbol)
  1905.     {
  1906.       XSET (tem1, Lisp_String, XSYMBOL (definition)->name);
  1907.       insert1 (tem1);
  1908.       insert_string ("\n");
  1909.     }
  1910.   else
  1911.     {
  1912.       tem1 = Fkeymapp (definition);
  1913.       if (!NILP (tem1))
  1914.     insert_string ("Prefix Command\n");
  1915.       else
  1916.     insert_string ("??\n");
  1917.     }
  1918. }
  1919.  
  1920. /* Describe the contents of map MAP, assuming that this map itself is
  1921.    reached by the sequence of prefix keys KEYS (a string or vector).
  1922.    PARTIAL, SHADOW is as in `describe_map_tree' above.  */
  1923.  
  1924. static void
  1925. describe_map (map, keys, partial, shadow)
  1926.      Lisp_Object map, keys;
  1927.      int partial;
  1928.      Lisp_Object shadow;
  1929. {
  1930.   register Lisp_Object keysdesc;
  1931.  
  1932.   if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
  1933.     {
  1934.       Lisp_Object tem;
  1935.       /* Call Fkey_description first, to avoid GC bug for the other string.  */
  1936.       tem = Fkey_description (keys);
  1937.       keysdesc = concat2 (tem, build_string (" "));
  1938.     }
  1939.   else
  1940.     keysdesc = Qnil;
  1941.  
  1942.   describe_map_2 (map, keysdesc, describe_command, partial, shadow);
  1943. }
  1944.  
  1945. /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
  1946.    Returns the first non-nil binding found in any of those maps.  */
  1947.  
  1948. static Lisp_Object
  1949. shadow_lookup (shadow, key, flag)
  1950.      Lisp_Object shadow, key, flag;
  1951. {
  1952.   Lisp_Object tail, value;
  1953.  
  1954.   for (tail = shadow; CONSP (tail); tail = XCONS (tail)->cdr)
  1955.     {
  1956.       value = Flookup_key (XCONS (tail)->car, key, flag);
  1957.       if (!NILP (value))
  1958.     return value;
  1959.     }
  1960.   return Qnil;
  1961. }
  1962.  
  1963. /* Insert a description of KEYMAP into the current buffer.  */
  1964.  
  1965. static void
  1966. describe_map_2 (keymap, elt_prefix, elt_describer, partial, shadow)
  1967.      register Lisp_Object keymap;
  1968.      Lisp_Object elt_prefix;
  1969.      void (*elt_describer) _P_((Lisp_Object desc));
  1970.      int partial;
  1971.      Lisp_Object shadow;
  1972. {
  1973.   Lisp_Object tail, definition, event;
  1974.   Lisp_Object tem;
  1975.   Lisp_Object suppress;
  1976.   Lisp_Object kludge;
  1977.   int first = 1;
  1978.   struct gcpro gcpro1, gcpro2, gcpro3;
  1979.  
  1980.   if (partial)
  1981.     suppress = intern ("suppress-keymap");
  1982.  
  1983.   /* This vector gets used to present single keys to Flookup_key.  Since
  1984.      that is done once per keymap element, we don't want to cons up a
  1985.      fresh vector every time.  */
  1986.   kludge = Fmake_vector (make_number (1), Qnil);
  1987.   definition = Qnil;
  1988.  
  1989.   GCPRO3 (elt_prefix, definition, kludge);
  1990.  
  1991.   for (tail = XCONS (keymap)->cdr; CONSP (tail); tail = Fcdr (tail))
  1992.     {
  1993.       QUIT;
  1994.  
  1995.       if (XTYPE (XCONS (tail)->car) == Lisp_Vector)
  1996.     describe_vector (XCONS (tail)->car,
  1997.              elt_prefix, elt_describer, partial, shadow);
  1998.       else
  1999.     {
  2000.       event = Fcar_safe (Fcar (tail));
  2001.       definition = get_keyelt (Fcdr_safe (Fcar (tail)));
  2002.  
  2003.       /* Don't show undefined commands or suppressed commands.  */
  2004.       if (NILP (definition)) continue;
  2005.       if (XTYPE (definition) == Lisp_Symbol && partial)
  2006.         {
  2007.           tem = Fget (definition, suppress);
  2008.           if (!NILP (tem))
  2009.         continue;
  2010.         }
  2011.  
  2012.       /* Don't show a command that isn't really visible
  2013.          because a local definition of the same key shadows it.  */
  2014.  
  2015.       XVECTOR (kludge)->contents[0] = event;
  2016.       if (!NILP (shadow))
  2017.         {
  2018.           tem = shadow_lookup (shadow, kludge, Qt);
  2019.           if (!NILP (tem)) continue;
  2020.         }
  2021.  
  2022.       tem = Flookup_key (keymap, kludge, Qt);
  2023.       if (! EQ (tem, definition)) continue;
  2024.  
  2025.       if (first)
  2026.         {
  2027.           insert ("\n", 1);
  2028.           first = 0;
  2029.         }
  2030.  
  2031.       if (!NILP (elt_prefix))
  2032.         insert1 (elt_prefix);
  2033.  
  2034.       /* THIS gets the string to describe the character EVENT.  */
  2035.       insert1 (Fsingle_key_description (event));
  2036.  
  2037.       /* Print a description of the definition of this character.
  2038.          elt_describer will take care of spacing out far enough
  2039.          for alignment purposes.  */
  2040.       (*elt_describer) (definition);
  2041.     }
  2042.     }
  2043.  
  2044.   UNGCPRO;
  2045. }
  2046.  
  2047. static _VOID_
  2048. describe_vector_princ (elt)
  2049.      Lisp_Object elt;
  2050. {
  2051.   Findent_to (make_number (16), make_number (1));
  2052.   Fprinc (elt, Qnil);
  2053.   Fterpri (Qnil);
  2054. }
  2055.  
  2056. DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
  2057.   "Insert a description of contents of VECTOR.\n\
  2058. This is text showing the elements of vector matched against indices.")
  2059.   (vector)
  2060.      Lisp_Object vector;
  2061. {
  2062.   int count = specpdl_ptr - specpdl;
  2063.  
  2064.   specbind (Qstandard_output, Fcurrent_buffer ());
  2065.   CHECK_VECTOR (vector, 0);
  2066.   describe_vector (vector, Qnil, describe_vector_princ, 0, Qnil);
  2067.  
  2068.   return unbind_to (count, Qnil);
  2069. }
  2070.  
  2071. _VOID_
  2072. describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
  2073.      register Lisp_Object vector;
  2074.      Lisp_Object elt_prefix;
  2075.      void (*elt_describer) _P_((Lisp_Object desc));
  2076.      int partial;
  2077.      Lisp_Object shadow;
  2078. {
  2079.   Lisp_Object this;
  2080.   Lisp_Object dummy;
  2081.   Lisp_Object tem1, tem2;
  2082.   register int i;
  2083.   Lisp_Object suppress;
  2084.   Lisp_Object kludge;
  2085.   int first = 1;
  2086.   struct gcpro gcpro1, gcpro2, gcpro3;
  2087.  
  2088.   tem1 = Qnil;
  2089.  
  2090.   /* This vector gets used to present single keys to Flookup_key.  Since
  2091.      that is done once per vector element, we don't want to cons up a
  2092.      fresh vector every time.  */
  2093.   kludge = Fmake_vector (make_number (1), Qnil);
  2094.   GCPRO3 (elt_prefix, tem1, kludge);
  2095.  
  2096.   if (partial)
  2097.     suppress = intern ("suppress-keymap");
  2098.  
  2099.   for (i = 0; i < XVECTOR (vector)->size; i++)
  2100.     {
  2101.       QUIT;
  2102.       tem1 = get_keyelt (XVECTOR (vector)->contents[i]);
  2103.  
  2104.       if (NILP (tem1)) continue;      
  2105.  
  2106.       /* Don't mention suppressed commands.  */
  2107.       if (XTYPE (tem1) == Lisp_Symbol && partial)
  2108.     {
  2109.       this = Fget (tem1, suppress);
  2110.       if (!NILP (this))
  2111.         continue;
  2112.     }
  2113.  
  2114.       /* If this command in this map is shadowed by some other map,
  2115.      ignore it.  */
  2116.       if (!NILP (shadow))
  2117.     {
  2118.       Lisp_Object tem;
  2119.       
  2120.       XVECTOR (kludge)->contents[0] = make_number (i);
  2121.       tem = shadow_lookup (shadow, kludge, Qt);
  2122.  
  2123.       if (!NILP (tem)) continue;
  2124.     }
  2125.  
  2126.       if (first)
  2127.     {
  2128.       insert ("\n", 1);
  2129.       first = 0;
  2130.     }
  2131.  
  2132.       /* Output the prefix that applies to every entry in this map.  */
  2133.       if (!NILP (elt_prefix))
  2134.     insert1 (elt_prefix);
  2135.  
  2136.       /* Get the string to describe the character I, and print it.  */
  2137.       XFASTINT (dummy) = i;
  2138.  
  2139.       /* THIS gets the string to describe the character DUMMY.  */
  2140.       this = Fsingle_key_description (dummy);
  2141.       insert1 (this);
  2142.  
  2143.       /* Find all consecutive characters that have the same definition.  */
  2144.       while (i + 1 < XVECTOR (vector)->size
  2145.          && (tem2 = get_keyelt (XVECTOR (vector)->contents[i+1]),
  2146.          EQ (tem2, tem1)))
  2147.     i++;
  2148.  
  2149.       /* If we have a range of more than one character,
  2150.      print where the range reaches to.  */
  2151.  
  2152.       if (i != XINT (dummy))
  2153.     {
  2154.       insert (" .. ", 4);
  2155.       if (!NILP (elt_prefix))
  2156.         insert1 (elt_prefix);
  2157.  
  2158.       XFASTINT (dummy) = i;
  2159.       insert1 (Fsingle_key_description (dummy));
  2160.     }
  2161.  
  2162.       /* Print a description of the definition of this character.
  2163.      elt_describer will take care of spacing out far enough
  2164.      for alignment purposes.  */
  2165.       (*elt_describer) (tem1);
  2166.     }
  2167.  
  2168.   UNGCPRO;
  2169. }
  2170.  
  2171. /* Apropos - finding all symbols whose names match a regexp.        */
  2172. Lisp_Object apropos_predicate;
  2173. Lisp_Object apropos_accumulate;
  2174.  
  2175. static void
  2176. apropos_accum (symbol, string)
  2177.      Lisp_Object symbol, string;
  2178. {
  2179.   register Lisp_Object tem;
  2180.  
  2181.   tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
  2182.   if (!NILP (tem) && !NILP (apropos_predicate))
  2183.     tem = call1 (apropos_predicate, symbol);
  2184.   if (!NILP (tem))
  2185.     apropos_accumulate = Fcons (symbol, apropos_accumulate);
  2186. }
  2187.  
  2188. DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0, 
  2189.   "Show all symbols whose names contain match for REGEXP.\n\
  2190. If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
  2191. for each symbol and a symbol is mentioned only if that returns non-nil.\n\
  2192. Return list of symbols found.")
  2193.   (string, pred)
  2194.      Lisp_Object string, pred;
  2195. {
  2196.   struct gcpro gcpro1, gcpro2;
  2197.   CHECK_STRING (string, 0);
  2198.   apropos_predicate = pred;
  2199.   GCPRO2 (apropos_predicate, apropos_accumulate);
  2200.   apropos_accumulate = Qnil;
  2201.   map_obarray (Vobarray, apropos_accum, string);
  2202.   apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
  2203.   UNGCPRO;
  2204.   return apropos_accumulate;
  2205. }
  2206.  
  2207. _VOID_
  2208. syms_of_keymap ()
  2209. {
  2210.   Qkeymap = intern ("keymap");
  2211.   staticpro (&Qkeymap);
  2212.  
  2213. /* Initialize the keymaps standardly used.
  2214.    Each one is the value of a Lisp variable, and is also
  2215.    pointed to by a C variable */
  2216.  
  2217.   global_map = Fcons (Qkeymap,
  2218.               Fcons (Fmake_vector (make_number (0400), Qnil), Qnil));
  2219.   Fset (intern ("global-map"), global_map);
  2220.  
  2221.   meta_map = Fmake_keymap (Qnil);
  2222.   Fset (intern ("esc-map"), meta_map);
  2223.   Ffset (intern ("ESC-prefix"), meta_map);
  2224.  
  2225.   control_x_map = Fmake_keymap (Qnil);
  2226.   Fset (intern ("ctl-x-map"), control_x_map);
  2227.   Ffset (intern ("Control-X-prefix"), control_x_map);
  2228.  
  2229.   DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
  2230.     "Default keymap to use when reading from the minibuffer.");
  2231.   Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
  2232.  
  2233.   DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
  2234.     "Local keymap for the minibuffer when spaces are not allowed.");
  2235.   Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
  2236.  
  2237.   DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
  2238.     "Local keymap for minibuffer input with completion.");
  2239.   Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
  2240.  
  2241.   DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
  2242.     "Local keymap for minibuffer input with completion, for exact match.");
  2243.   Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
  2244.  
  2245.   current_global_map = global_map;
  2246.  
  2247.   DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
  2248.     "Alist of keymaps to use for minor modes.\n\
  2249. Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
  2250. key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
  2251. If two active keymaps bind the same key, the keymap appearing earlier\n\
  2252. in the list takes precedence.");
  2253.   Vminor_mode_map_alist = Qnil;
  2254.  
  2255.   DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
  2256.   "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
  2257. This allows Emacs to recognize function keys sent from ASCII\n\
  2258. terminals at any point in a key sequence.\n\
  2259. \n\
  2260. The read-key-sequence function replaces subsequences bound by\n\
  2261. function-key-map with their bindings.  When the current local and global\n\
  2262. keymaps have no binding for the current key sequence but\n\
  2263. function-key-map binds a suffix of the sequence to a vector or string,\n\
  2264. read-key-sequence replaces the matching suffix with its binding, and\n\
  2265. continues with the new sequence.\n\
  2266. \n\
  2267. For example, suppose function-key-map binds `ESC O P' to [f1].\n\
  2268. Typing `ESC O P' to read-key-sequence would return [f1].  Typing\n\
  2269. `C-x ESC O P' would return [?\\C-x f1].  If [f1] were a prefix\n\
  2270. key, typing `ESC O P x' would return [f1 x].");
  2271.   Vfunction_key_map = Fmake_sparse_keymap (Qnil);
  2272.  
  2273.   Qsingle_key_description = intern ("single-key-description");
  2274.   staticpro (&Qsingle_key_description);
  2275.  
  2276.   Qkey_description = intern ("key-description");
  2277.   staticpro (&Qkey_description);
  2278.  
  2279.   Qkeymapp = intern ("keymapp");
  2280.   staticpro (&Qkeymapp);
  2281.  
  2282.   Qnon_ascii = intern ("non-ascii");
  2283.   staticpro (&Qnon_ascii);
  2284.  
  2285.   defsubr (&Skeymapp);
  2286.   defsubr (&Smake_keymap);
  2287.   defsubr (&Smake_sparse_keymap);
  2288.   defsubr (&Scopy_keymap);
  2289.   defsubr (&Skey_binding);
  2290.   defsubr (&Slocal_key_binding);
  2291.   defsubr (&Sglobal_key_binding);
  2292.   defsubr (&Sminor_mode_key_binding);
  2293.   defsubr (&Sglobal_set_key);
  2294.   defsubr (&Slocal_set_key);
  2295.   defsubr (&Sdefine_key);
  2296.   defsubr (&Slookup_key);
  2297.   defsubr (&Sglobal_unset_key);
  2298.   defsubr (&Slocal_unset_key);
  2299.   defsubr (&Sdefine_prefix_command);
  2300.   defsubr (&Suse_global_map);
  2301.   defsubr (&Suse_local_map);
  2302.   defsubr (&Scurrent_local_map);
  2303.   defsubr (&Scurrent_global_map);
  2304.   defsubr (&Scurrent_minor_mode_maps);
  2305.   defsubr (&Saccessible_keymaps);
  2306.   defsubr (&Skey_description);
  2307.   defsubr (&Sdescribe_vector);
  2308.   defsubr (&Ssingle_key_description);
  2309.   defsubr (&Stext_char_description);
  2310.   defsubr (&Swhere_is_internal);
  2311.   defsubr (&Swhere_is);
  2312.   defsubr (&Sdescribe_bindings);
  2313.   defsubr (&Sapropos_internal);
  2314. }
  2315.  
  2316. _VOID_
  2317. keys_of_keymap ()
  2318. {
  2319.   initial_define_key (global_map, 033, "ESC-prefix");
  2320.   initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
  2321. }
  2322.